import { useRequest } from '@umijs/max';
import { useState } from 'react';

const useList = (api: any, form: any, params: any) => {
  const [count, setCount] = useState(1);
  const [limitNum, setlimitNum] = useState(10);
  const [search, setSearch] = useState('');

  const { data, error, loading } = useRequest(
    () => {
      return api({
        count: count,
        limitNum: limitNum,
        keyWord: search || undefined,
        // type: type || undefined,
        // flag: flag,
        ...params,
      });
    },
    {
      refreshDeps: [count, limitNum, search, ...Object.keys(params)],
    },
  );

  const reset = () => {
    form.resetFields();
    setSearch('');
  };

  const onFinish = (values: any) => {
    setSearch(values.proname);
  };

  return {
    count,
    setCount,
    limitNum,
    setlimitNum,
    data,
    loading,
    search,
    setSearch,
    reset,
    onFinish,
  };
};

export default useList;
